<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="用户昵称">
          <el-input
            v-model="listQuery.nickname"
            placeholder="请输入用户昵称"
          ></el-input>
        </el-form-item>

        <el-form-item label="用户号码">
          <el-input
            v-model="listQuery.mobile"
            placeholder="请输入用户号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="注册日期">
          <el-date-picker
            v-model="listQuery.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search" size="small"
            >查询</el-button
          >
          <el-button @click="reset" size="small">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="downloadLoading"
            type="success"
            icon="el-icon-download"
            @click="handleDownload"
            size="small"
          >
            数据导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      key="id"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
    >
      <el-table-column label="序号" type="index" width="50"> </el-table-column>

      <el-table-column label="头像" width="100px">
        <template slot-scope="{ row }">
          <el-avatar :src="row.avatar"></el-avatar>
        </template>
      </el-table-column>

      <el-table-column label="昵称" prop="nickname" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="手机号" prop="mobile"> </el-table-column>

      <el-table-column
        label="注册时间"
        prop="create_time"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="mini" @click="handleView(row)">
            查看资料
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <el-dialog width="500px" title="用户信息" :visible.sync="showDialog">
      <el-form class="modal-scroll" label-position="left" label-width="70px">
        <el-form-item label="用户头像">
          <el-avatar :src="info.avatar"></el-avatar>
        </el-form-item>
        <el-form-item label="用户昵称" prop="timestamp">{{
          info.nickname
        }}</el-form-item>
        <el-form-item label="手机号码" prop="title">{{
          info.mobile
        }}</el-form-item>
        <el-form-item label="用户姓名" prop="title">{{
          info.name
        }}</el-form-item>
        <el-form-item label="用户性别" prop="title">
          {{ info.sex }}
        </el-form-item>
        <el-form-item label="用户生日" prop="title">
          {{ info.birthday }}
        </el-form-item>
        <el-form-item label="微信号" prop="title">
          {{ info.wechat }}
        </el-form-item>
        <el-form-item label="电子邮箱" prop="title">
          {{ info.email }}
        </el-form-item>
        <el-form-item label="联系QQ" prop="title"> {{ info.qq }} </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { exportExcel, fetchList, getUserInfo } from "@/api/user";
import Pagination from "@/components/Pagination";

export default {
  name: "user",
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        nickname: undefined,
        mobile: undefined,
        date: undefined,
      },
      info: {},
      showDialog: false,
      downloadLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      const res = await fetchList(this.listQuery);
      if (res.data) {
        this.list = res.data.list;
        this.total = res.data.pageParam.total;
      }
      this.listLoading = false;
    },
    async getUser(user_id) {
      const res = await getUserInfo({ user_id });
      if (res.data) {
        this.info = res.data;
      }
    },

    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery = {
        page: 1,
        pageSize: 20,
        nickname: undefined,
        mobile: undefined,
        date: undefined,
      };
      this.getList();
    },
    // 查看资料
    handleView(row) {
      this.getUser(row.user_id);
      this.showDialog = true;
    },
    // 数据导出
    async handleDownload() {
      this.downloadLoading = true;
      const res = await exportExcel(this.listQuery);
      if (res) {
        this.downloadFile(res, "用户列表");
      }
      this.downloadLoading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.modal-scroll {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
